<?php

namespace App\Http\Controllers\Api;

use App\EcmsHtfComment;
use App\Http\Extend\Helper;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redis;

class EmpireController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function index(Request $request)
    {
        $page = $request->page ? $request->page : 1; // 开始页数
        $rows = 10; // 每页显示数目
        $start = ($page-1)*$rows;

        $eHtfComment = new EcmsHtfComment();
        //获取最热前三条
        $hotData = $eHtfComment->where('a_id',$request->id)->where('p_id',1)->where('sh',1)->orderBy('zan','DESC')->limit(3)->get()->toArray();
        if($page == 1)
        {
            $rows = 7; //第一页显示三条最热评论
        }
        $count = $eHtfComment->where('a_id',$request->id)->where('p_id',1)->where('sh',1)->count();
        $data = $eHtfComment->where('a_id',$request->id)->where('p_id',1)->where('sh',1)->whereNotIn('id',array_column($hotData,'id'))
            ->offset($start)->limit($rows)->orderBy('create_time','DESC')->get()->toArray();
        ($page == 1) ? $data = array_merge($hotData,$data) : ''; //第一页的时候才合并数据

        //查找评论
        $comment = $eHtfComment->where('a_id',$request->id)->where('sh',1)->whereIn('p_id',array_column($data,'id'))->get()->toArray();
        foreach ($data as $k => &$v)
        {
            $v['comment'] = [];
            if($comment)
            {
                foreach ($comment as $k2 => &$v2)
                {
                    if($v2['p_id'] == $v['id'])
                        $v['comment'][] = $v2;
                }
            }
        }

        return response()->json([
            'data' => $data,
            'count' => $count
        ]);
    }

    /**
     * 添加评论
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     * @throws \Exception
     */
    public function addComment(Request $request)
    {
        if(empty($request->yzm)) throw new \Exception('验证码不存在！');
        if($request->yzm != Cache::get('empire_'.$request->tel)) throw new \Exception('验证码不正确或者已经超时！');
        $ecmHtf = new EcmsHtfComment();
        //查看是否是二级评论
        $ecmHtf->p_id = $request->p_id ? $request->p_id : 1; //所在级别
        $ecmHtf->a_id = $request->a_id;
        $ecmHtf->tel = $request->tel;
        $ecmHtf->content = $request->input('content');
        $ecmHtf->create_time = date('Y-m-d H:i:s');
        $id = $ecmHtf->save();
        if($id) Cache::forget('empire_'.$request->tel); //清空一下保存的验证码

        return response()->json($id);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    /**
     * 发送验证码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function yzm(Request $request)
    {
//        if(Cache::get('empire_18736007158'))
//        {
//            App::abort(400, '验证码'.Cache::get('empire_18736007158').'已经存在'); //抛出异常
//        }
//        Cache::put('empire_18736007158','1234',2);

        Helper::sendMsg($request->tel);
        return response()->json([
            'message' => '验证码发送成功！'
        ]);
    }

    /**
     * 点赞
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * create by wenQing
     */
    public function zan(Request $request)
    {
        $re = EcmsHtfComment::where('id',$request->id)->increment('zan');
        return response()->json($re);
    }
}
